﻿using System;

namespace Wei8808Cms.WebSite.UserControl
{
    public partial class Pagination : System.Web.UI.UserControl
    {
        private int _pageindex;
        public int PageIndex
        {
            get
            {
                if (_pageindex == 0)
                {
                    _pageindex = 1;
                }
                return _pageindex;
            }
            set { _pageindex = value; }
        }
        //一页的大小
        private int _pageSize;
        public int PageSize
        {
            get
            {
                if (_pageSize == 0)
                {
                    _pageSize = 1;
                }
                return _pageSize;
            }
            set { _pageSize = value; }
        }
        //总记录数
        private int _recordCount;
        public int RecordCount
        {
            get { return _recordCount; }
            set { _recordCount = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataBind();
            }
        }
        //绑定
        public void LoadDataBind()
        {
            int pageCount = (RecordCount % PageSize) == 0 ? RecordCount / PageSize : RecordCount / PageSize + 1;//页数

            Initialization(pageCount);
        }
        //数据初始化
        private void Initialization(int pageCount)
        {
            //--------求出总记录数
            var record = RecordCount.ToString();
            //lbRecord.Text = RecordCount.ToString();
            //--------当前为第几页
            var row = PageIndex.ToString();
            //lbRow.Text = PageIndex.ToString();
            //--------共多少页    
            var page = pageCount.ToString();
            //lbpage.Text = pageCount.ToString();

            this.first.Enabled = true;
            this.prev.Enabled = true;
            this.next.Enabled = true;
            this.lend.Enabled = true;

            if (Convert.ToInt32(row) == 1)
            {
                this.first.Enabled = false;
                this.prev.Enabled = false;
            }
            if (Convert.ToInt32(row) == pageCount)
            {
                this.next.Enabled = false;
                this.lend.Enabled = false;
            }

            this.first.CommandArgument = "1";
            this.prev.CommandArgument = (PageIndex - 1).ToString();
            this.next.CommandArgument = (PageIndex + 1).ToString();
            this.lend.CommandArgument = pageCount.ToString();

        }

        //定义一个委托
        public delegate void reBindEvent(object sender, EventArgs arg);
        public event reBindEvent reBind;

        protected void reBind_Click(object sender, EventArgs e)
        {
            if (this.reBind != null)
                this.reBind(sender, e);
            LoadDataBind();
        }
    }
}